﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Wise.Entities;

namespace NHI.Sync.Tools.Forms
{
    /// <summary>
    /// LoadConfig.xaml 的交互逻辑
    /// </summary>
    public partial class Form_LoadConfig : UserControl
    {
      
        public Form_LoadConfig()
        {
            InitializeComponent();
            string binDir = FileHelper.GetCurrentDirectory();
            string[] files = System.IO.Directory.GetFiles(System.IO.Path.Combine(binDir, "Configs"), "默认配置文件.json", System.IO.SearchOption.AllDirectories);
            if (files.Length > 0)
            {
                try
                {
                    MyDbConfig.Current = new MyDbConfig(files.FirstOrDefault());
                }
                catch (Exception exp)
                {
                    MessageBox.Show($"在加载默认配置文件时,发生{exp.Message}");
                }
            }
        }


        protected void OnPrevious_Click(object o, EventArgs e)
        {
        }

        public void OnNext_Click(object o, EventArgs e)
        {
            if (MyDbConfig.Current == null)
                MyDbConfig.Current = new MyDbConfig();
            MainWindow.Current.tab_1.IsEnabled = true;
            MainWindow.Current.tab_1.IsSelected = true;
            MainWindow.Current.frame_1.LoadData();
            if (MyDbConfig.Current.是测试)
                MainWindow.Current.frame_1.OnNext_Click(o, e);
        }

        public void DoLoadConfig(object o, EventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = ".json";
            ofd.Filter = "数据迁移配置文件|*.json";
            ofd.Title = "打开配置文件";
            if (ofd.ShowDialog() == true)
            {
                try
                {
                    MyDbConfig.Current = new MyDbConfig(ofd.FileName);
                }
                catch (Exception exp)
                {
                    MyDbConfig.Current = new MyDbConfig();
                    MessageBox.Show(exp.Message);
                }
                OnNext_Click(o, e);
            }
        }
        public void DoLoadMapping(object o, EventArgs e)
        {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.DefaultExt = ".xlsx";
            ofd.Filter = "数据字段映射文件|*.xlsx;*.xls";
            ofd.Title = "打开数据库表映射文件";
            if (ofd.ShowDialog() == true)
            {
                MyDbConfig.Current = new MyDbConfig();
                try
                {
                    MyDbConfig.Current.LoadMap(ofd.FileName);
                }
                catch (Exception exp)
                {
                    MessageBox.Show(exp.Message);
                }
                //MyDbConfig.Current.表映射.AddRange()
                OnNext_Click(o, e);
            }
        }
    }
}
